Skip to main content

Update Entities with Jenkins

This page demonstrates how to use a Jenkins pipeline to gather all commit comments from a build and update all related entities mentioned in those commits using the SyncNow DevOps Gate API.

API requests are implemented using the HTTP Request Plugin and a declarative Jenkinsfile.


📝 Jenkins Pipeline Example

The following script collects commit messages, formats them, and sends a POST request to SyncNow to update all referenced entities.

@NonCPS
def getCommentsString() {
    def list = []
    def changeLogSets = currentBuild.changeSets
    for (int i = 0; i < changeLogSets.size(); i++) {
        def entries = changeLogSets[i].items
        for (int j = 0; j < entries.length; j++) {
            def entry = entries[j]
            if (entry.msg != null) {
                list.add("""${entry.commitId} on ${new Date(entry.timestamp)}: ${entry.msg.replaceAll("[\n\r]", " ")}""")
            }
        }
    }
    return list.join(',')
}

pipeline {
    agent any

    stages {
        stage('Clone') { steps { echo 'Clone Code' } }
        stage('Version') { steps { echo 'Version' } }
        stage('Test') { steps { echo 'Test' } }
        stage('Build') { steps { echo 'Build' } }
        stage('Publish') { steps { echo 'Publish' } }
        stage('Notify SyncNow') {
            steps {
                echo 'Notify'
                echo "Job Name is ${JOB_NAME}, Build ID is ${env.BUILD_ID}"
                script {
                    def commits = getCommentsString()
                    def payload = """
[
  {
    "fields": [
      { "key": "Param1", "value": "string" },
      { "key": "Param2", "value": "string" },
      { "key": "Param3", "value": "string" }
    ],
    "entityType": "Bug",
    "comments": "${commits}"
  }
]
                    """
                    // Create or update entities from commit comments
                    def response = httpRequest(
                        acceptType: 'APPLICATION_JSON',
                        contentType: 'APPLICATION_JSON',
                        httpMode: 'POST',
                        authentication: 'SyncNow',
                        requestBody: "${payload}",
                        url: "https://syncnowserver/api/v1.0/DevOpsGate/Enrich/<DevOpsGateProcessID>?action=update"
                    )
                    println("Status: ${response.status}")
                    println("Content: ${response.content}")
                }
            }
        }
    }
}

💡 How It Works

  • Commit Parsing:
    The getCommentsString() function collects all commit messages from the current build and formats them for SyncNow.
  • Payload Construction:
    The payload includes fields and the collected comments, which reference entities to be updated.
  • API Call:
    The pipeline sends a POST request to the SyncNow DevOps Gate API, updating all entities mentioned in the commit messages.

Tip:
Use this approach to automate entity updates in your work systems based on code changes, improving traceability and collaboration between DevOps and work management tools.